package com.woniuxy.web;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.entity.Address;
import com.woniuxy.entity.Cart;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.AddressService;
import com.woniuxy.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import static java.lang.Integer.parseInt;

@Controller
@RequestMapping("cart_vue")
public class CartController_vue {

    @Autowired
    private CartService cartService;
    @Autowired
    private AddressService addressService;

    /**
     * 进入购物车    展示用户购物车中商品
     */
    @GetMapping("cart")
    public String cart(){
        return "cart_vue";
    }
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<Cart>> cart(HttpSession session) {
        User user = (User) session.getAttribute("user");
        int id = user.getId();
        List<Cart> carts = cartService.getByCondition(id);
        ResponseEntity<List<Cart>> responseEntity = new ResponseEntity<>();
        responseEntity.setCode(200);
        responseEntity.setData(carts);
      /*  for (Cart cart : carts) {
            System.out.println(cart);
        }*/
        return responseEntity;
    }

    /**
     * 更改购物车中商品数量
     */
    @PostMapping("upnum")
    @ResponseBody
    public ResponseEntity reduce(@RequestBody Cart cart) {
        /*System.out.println(cart.getDishId()+"+++++++++++++++++++++++++++++"+cart.getNums()+"++++++++++++++++"+cart.getId());*/
       ResponseEntity responseEntity = new ResponseEntity();
       if (cart.getNums() >= 0) {
            UpdateWrapper<Cart> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", cart.getId());
            cartService.update(cart, wrapper);
            return ResponseEntity.SUCCESS;
        } else {
            //数量少于等于0    返回状态码501
            responseEntity.setCode(501);
            responseEntity.setMsg("数量不能小于0");
            return responseEntity;
        }
    }

    /**   购物车跳转订单提交页面      展示商品    地址    */
    @GetMapping("order")
    public String order(){
        return "confirm_order_vue";
    }

    @PostMapping("order/list")
    @ResponseBody
    public ResponseEntity<List> orderlist(@RequestBody List<Cart> carts, HttpSession session) {
        User user = (User) session.getAttribute("user");
        ResponseEntity objectResponseEntity = new ResponseEntity();
        List list = new ArrayList();
        //根据用户ID查找收货地址
        List<Address> addrs = addressService.getByAddr(user.getId());
        list.add(addrs);

        //根据用户购物车中选中的购物车ID查找相应的商品
        /*String[] ids = request.getParameterValues("id");*/
        List<Integer> ids=new ArrayList<>();
        for (Cart cart : carts) {
            ids.add(cart.getId());
        }
        List<Cart> dishs = new ArrayList<>();
        for (Integer id : ids) {
            dishs.add(cartService.getByDish(user.getId(), id));
        }
        list.add(dishs);
        //计算总价
       /* BigDecimal zprice = new BigDecimal("0.00");
        for (Cart dish : dishs) {
            System.out.println(dish.getNums() + "+++++++++" + "++++++++++" + dish.getDish().getPrice());
            BigDecimal num = new BigDecimal(dish.getNums() + "");
            BigDecimal price = new BigDecimal(dish.getDish().getPrice() + "");
            BigDecimal z = num.multiply(price);
            System.out.println(z);
            zprice = zprice.add(z);
            System.out.println(zprice);
        }*/
        /*System.out.println(zprice);
        list.add(zprice);*/
        return new ResponseEntity<List>(list);
    }

    /**
     * 菜品直接加入购物车
     */
    @PostMapping("add")
    public String add(HttpServletRequest request,HttpSession session) {
        String dishid = request.getParameter("dishid");
        String number = request.getParameter("Number");
        User user = (User)session.getAttribute("user");
        Cart cart = new Cart();
        //判断此商品是否存在于用户购物车中
        int sel = cartService.sel(user.getId(), parseInt(dishid));
        if(sel>0){
            Cart selcart = cartService.selCart(user.getId(), parseInt(dishid));
            Integer nums = selcart.getNums();
            selcart.setNums(nums+Integer.parseInt(number));
            cartService.updateById(selcart);
        }else{
            cart.setDishId(Integer.parseInt(dishid));
            cart.setSelected("y");
            cart.setUserId(user.getId());
            cart.setNums(Integer.parseInt(number));
            //加一个选择规格后计算的价格    数据库加一列

            cartService.save(cart);
        }
        return "redirect:/cart/list";
    }

    @GetMapping("del")
    @ResponseBody
    public ResponseEntity del(int id,HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user.getId()!=0){
            cartService.del(id,user.getId());
            return ResponseEntity.SUCCESS;
        }else{
            ResponseEntity responseEntity = new ResponseEntity();
            responseEntity.setCode(501);
            responseEntity.setMsg("没有登录");
            return responseEntity;
        }

    }
}
